package com.dragon.modules.yuegou.client.wechat.response;

import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.enums.code.LiveCode;
import com.dragon.modules.yuegou.enums.code.LiveUtils;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;

public class OpenidResponse implements Serializable {
	private static final long serialVersionUID = 1L;

	// 用户唯一标识
	private String openid;

	// 会话密钥
	private String session_key;

	// 用户在开放平台的唯一标识符，在满足 UnionID 下发条件的情况下会返回，详见 UnionID 机制说明。
	private String unionid;

	// 错误码
	private Integer errcode;

	// 错误信息
	private String errmsg;
	
	@JsonProperty("expire_in")
	private Integer expireIn;

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public String getSession_key() {
		return session_key;
	}

	public void setSession_key(String session_key) {
		this.session_key = session_key;
	}

	public String getUnionid() {
		return unionid;
	}

	public void setUnionid(String unionid) {
		this.unionid = unionid;
	}

	public Integer getErrcode() {
		return errcode;
	}

	public void setErrcode(Integer errcode) {
		this.errcode = errcode;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public void setErrmsg(String errmsg) {
		this.errmsg = errmsg;
	}

	public Integer getExpireIn() {
		return expireIn;
	}

	public void setExpireIn(Integer expireIn) {
		this.expireIn = expireIn;
	}

	// 响应较验
	public void check() {
		if (null != errcode && errcode.intValue() != 0) {
			LiveCode liveCode = LiveUtils.getLiveCode(errcode);
			if (liveCode == null) {
				throw new DragonException(LiveCode.LIVE_ERROR.getCode(),String.format(LiveCode.LIVE_ERROR.getMessage(),errmsg));
			}else {
				throw new DragonException(liveCode);
			}

		}
	}
}
